package com.example.mallmember.controller;

import com.common.utils.PageUtils;
import com.common.utils.R;
import com.common.vo.MemberVO;
import com.example.mallmember.controller.vo.MemberLoginVO;
import com.example.mallmember.controller.vo.MemberReigerVO;
import com.example.mallmember.entity.MemberEntity;
import com.example.mallmember.entity.MemberReceiveAddressEntity;
import com.example.mallmember.exception.PhoneExsitExecption;
import com.example.mallmember.exception.UsernameExsitException;
import com.example.mallmember.service.MemberService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 会员
 *
 * @author charlie
 * @email @gmail.com
 * @date 2023-11-25 12:57:40
 */
@RestController
@RequestMapping("mallmember/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


    /**
     * 会员注册
     *
     * @return
     */
    @PostMapping("/register")
    public R register(@RequestBody MemberReigerVO vo) {
        try {
            memberService.register(vo);
        } catch (UsernameExsitException exception) {
            return R.error(10022,
                    "账号名错误");
        } catch (PhoneExsitExecption exsitExecption) {
            return R.error(10023,
                    "手机号错误");
        } catch (Exception e) {
            return R.error(10021,
                    e.getMessage());
        }

        return R.ok();
    }

    @Autowired
    ObjectMapper objectMapper;

    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVO vo) throws JsonProcessingException {
        MemberEntity entity = memberService.login(vo);
        if (entity != null) {
            MemberVO memberVO = new MemberVO();
            memberVO.setUsername(entity.getUsername());
            memberVO.setPassword(entity.getPassword());
            memberVO.setNickname(entity.getNickname());
            memberVO.setMobile(entity.getMobile());
            memberVO.setId(entity.getId());
            return R.ok().put("data", objectMapper.writeValueAsString(memberVO));
        }

        return R.error(10024,
                "登录错误");
    }

    @GetMapping("/{memberId}/address")
    public List<MemberReceiveAddressEntity> getAddress(@PathVariable("memberId") Long memberId) {
        List<MemberReceiveAddressEntity> memberAddressList = memberService.getAddress(memberId);
        return memberAddressList;
    }

}
